//index.js
/* import Dialog from '@vant/weapp/dialog/dialog'; */
const App = getApp()

Page({
  data: {
    List: [],
    loading: true,
    vip1: '--',
    vip3: null,
    showDialog: false
  },
  async onLoad(opt) {
    if (opt.promotioncode) {
      wx.setStorageSync('promotioncode', opt.promotioncode)
    }
    //let { windowWidth } = wx.getWindowInfo();
    let isLogin = wx.getStorageSync('token');
    if (!isLogin) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        isLogin: true
      }, async () => {
        await this.usertimes();
      });
    }
  },
  onShow() {
    let firstLogin = wx.getStorageSync('firstLogin');
    if (firstLogin === 1) {
      wx.setStorageSync('firstLogin', 0);
      this.onPullDownRefresh();
    }
    let ischange = wx.getStorageSync('ischange');
    if (ischange === 1) {
      wx.setStorageSync('ischange', 0);
      this.onPullDownRefresh();
    }

  },
  onHide() {},
  onUnload() {},
  //关闭截图
  offCaptureScreen() {
    let {
      vip3
    } = this.data;
    // console.log(vip3)
    if (vip3 != null && vip3 != '已过期') {
      if (wx.setVisualEffectOnCapture) {
        //安卓截屏时提示禁止截屏
        wx.setVisualEffectOnCapture({
          visualEffect: 'hidden',
        });
      }
      //iOS截屏时断掉
      wx.onUserCaptureScreen(() => {
        console.log('用户截屏了')
        this.setData({
          List: []
        });
      });
      //机子录屏时
      wx.onScreenRecordingStateChanged(() => {
        console.log('用户录屏了')
        this.setData({
          List: []
        });
      });
    };
  },
  async onPullDownRefresh() {
    let isLogin = wx.getStorageSync('token');
    if (!isLogin) {
      this.setData({
        showDialog2: true,
        tipsText: '用户未登录，无法刷新',
      });
      wx.stopPullDownRefresh();
      return
    }
    this.setData({
      loading: true
    });
    await this.usertimes();
    wx.stopPullDownRefresh();
  },
  async usertimes() {
    App.showLoading();
    let {
      data
    } = await App.$http("/api/front/userviptimes");
    this.setData({
      vip1: data.vip1,
      vip3: data.vip3deadline,
      loading: false
    },() => {
      this.offCaptureScreen();
    });
    wx.hideLoading();
  },
  onListDialog() {
    let {
      vip1,
      vip3
    } = this.data;
    if (vip1 === '--') {
      this.setData({
        showDialog2: true,
        tipsText: '请您到【我的】页面，点击登录后再使用此功能',
      });
      return
    }
    if (vip1 <= 0 && (vip3 === null || vip3 === '已过期')) {
      this.setData({
        showDialog2: true,
        tipsText: '您的使用次数不足，无法解锁列表',
      });
      return
    }
    this.setData({
      showDialog: true
    });
  },
  onCloseDialog2() {
    this.setData({
      showDialog2: false
    });
  },
  onCloseDialog() {
    this.setData({
      showDialog: false
    });
  },
  async requestData() {
    let {
      vip1
    } = this.data;
    try {
      let {
        data
      } = await App.$http("/api/front/getTicaiList", {
        date: '2023-07-18'
      });
      this.setData({
        List: data.arrItems,
        showDialog: false,
        loading: false,
        vip1: data.vip1
      });
      wx.hideLoading();
    } catch (error) {
      this.setData({
        showDialog2: true,
        tipsText: error,
      });
      wx.hideLoading();
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let {
      promotioncode
    } = wx.getStorageSync('user')
    return {
      title: '足球比赛预测计算器',
      path: promotioncode ? `/pages/index/index?promotioncode=${promotioncode}` : `/pages/index/index`,
      imageUrl: '../../images/logo_share.jpg'
    }
  },
  onShareTimeline() {
    let {
      promotioncode
    } = wx.getStorageSync('user')
    // console.log(promotioncode)
    return {
      title: '今日竞彩计算',
      query: promotioncode ? `promotioncode=${promotioncode}&q=code` : ``,
      imageUrl: '../../images/logo_share.jpg'
    }
  }
});